home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
aminet
/
util
/
gnu
/
gnu_tile_forth.lha
/
lib
/
queues.f83
< prev
next >
Wrap
Text File
|
1992-05-19
|
4KB
|
136 lines
\
\ DOUBLE LINKED LISTS
\
\ Copyright (C) 1988-1990 by Mikael R.K. Patel
\
\ Computer Aided Design Laboratory (CADLAB)
\ Department of Computer and Information Science
\ Linkoping University
\ S-581 83 LINKOPING
\ SWEDEN
\
\ Email: mip@ida.liu.se
\
\ Started on: 30 June 1988
\
\ Last updated on: 23 July 1990
\
\ Dependencies:
\ (forth) forth, structures, blocks
\
\ Description:
\ Allows definition and basic manipulation of queue data structures.
\
\ Copying:
\ This program is free software; you can redistribute it and\or modify
\ it under the terms of the GNU General Public License as published by
\ the Free Software Foundation; either version 1, or (at your option)
\ any later version.
\
\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\ GNU General Public License for more details.
\
\ You should have received a copy of the GNU General Public License
\ along with this program; see the file COPYING. If not, write to
\ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
.( Loading Queues definitions... ) cr
#include structures.f83
#include blocks.f83
blocks structures queues definitions
struct.type QUEUE ( -- )
ptr +succ ( queue -- addr) private
ptr +pred ( queue -- addr) private
struct.init ( queue -- )
dup over +succ ! dup +pred !
struct.end
: succ ( queue -- succ)
+succ @
;
: pred ( queue -- pred)
+pred @
;
#ifundef ?empty-queue ( Check if the kernel supports queues)
: ?empty-queue ( queue -- bool)
dup +succ @ = ( Pointer to itself)
;
: enqueue ( item queue -- )
2dup +pred @ swap +pred ! ( item.pred = queue.pred)
2dup swap +succ ! ( item.succ = queue)
2dup +pred @ +succ ! ( queue.pred.succ = item)
+pred ! ( queue.pred = item)
;
: dequeue ( item -- )
dup +succ @ over +pred @ +succ ! ( item.pred.succ = item.succ)
dup +pred @ over +succ @ +pred ! ( item.succ.pred = item.pred)
dup over +succ ! ( item.succ = item)
dup +pred ! ( item.pred = item)
;
#then
: size-queue ( queue -- num)
0 swap dup >r ( Save pointer to queue header)
begin
swap 1+ swap +succ @ ( Increment size and step to next)
dup r@ = ( Is this the last element?)
until
r> 2drop ( Drop parameters and return size)
;
: map-queue ( queue block[item -- ] -- )
over >r ( Save pointer to queue header)
begin
over +succ @ >r ( Save pointer to next item)
dup >r ( Save block on return stack)
call ( Call the block with the item)
2r> tuck ( Restore the parameters)
r@ = ( Check if end of queue)
until
r> drop 2drop ( Drop all temporary parameters)
;
: ?map-queue ( queue block[item -- bool] -- )
over >r ( Save pointer to queue header)
begin
over +succ @ >r ( Save pointer to next item)
dup >r ( Save block on return stack)
call ( Call the block with the item)
if 2r> true ( Exit the iteration)
else
2r> tuck ( Restore the parameters)
r@ = ( Check if end of queue)
then
until
r> drop 2drop ( Drop all temporary parameters)
;
: ?member-queue ( element queue -- bool)
dup >r ( Save pointer to queue header)
begin
2dup = ( Is this the element?)
if 2drop r> drop true exit then ( Well drop the parameters and return)
+succ @ dup r@ = ( Step to the next. Last element?)
until
r> drop 2drop false
;
: .queue ( queue -- )
." queue#" dup . ( Print address of queue)
." succ: " dup +succ @ . ( Print successor)
." pred: " +pred @ . ( Print predecessor)
;
forth only